package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelLabelMapper;
import com.heima.admin.mapper.LabelMapper;
import com.heima.admin.service.ChannelLabelService;
import com.heima.apis.admin.IAdminChannelClient;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.ChannelLabel;
import com.heima.model.admin.pojos.Label;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;


/**
 * <p>
 * 频道标签信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class ChannelLabelServiceImpl extends ServiceImpl<ChannelLabelMapper, ChannelLabel> implements ChannelLabelService {
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private IAdminChannelClient adminClient;

    /**
     * 新增pingdao
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult add(WmChannel wmChannel) {
        if (null == wmChannel) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Label selectOne = labelMapper.selectOne(Wrappers.<Label>lambdaQuery().eq(Label::getName, wmChannel.getName()));
        if (null != selectOne) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道的名称已经存在,无法添加");
        }
        WmChannel channel = new WmChannel();
        BeanUtils.copyProperties(wmChannel, channel);
        channel.setCreatedTime(new Date());
        channel.setIsDefault(true);
        ResponseResult result = adminClient.addchannel(channel);
        Integer channel_id = (Integer) result.getData();
        Label label = new Label();
        BeanUtils.copyProperties(wmChannel, label);
        label.setType(false);
        label.setCreatedTime(new Date());
        labelMapper.insert(label);

        ChannelLabel channelLabel = new ChannelLabel();
        BeanUtils.copyProperties(wmChannel, channelLabel);
        channelLabel.setChannelId(channel_id);
        channelLabel.setLabelId(label.getId());
        //channelLabel.setOrd(0);
        save(channelLabel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询频道
     * @param channelDto
     * @return
     */
    @Override
    public ResponseResult getlist(ChannelDto channelDto) {
        return adminClient.getList(channelDto);
    }

    @Override//传递的channel ID
    public ResponseResult delete(Integer id) {
        ResponseResult resultdelete = adminClient.delete(id);//删除wemedia表中的数据
        if (resultdelete.getCode()!=200){
            return  ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        }
        ChannelLabel channelLabel = this.getOne(Wrappers.<ChannelLabel>lambdaQuery().eq(ChannelLabel::getChannelId, id));
        Integer labelId = channelLabel.getLabelId();
        boolean result = this.remove(Wrappers.<ChannelLabel>lambdaQuery().eq(ChannelLabel::getChannelId, id));
        if (!result){
            return ResponseResult.errorResult(AppHttpCodeEnum.EXCEL_REPORT_FAIL);
        }
        if (null==labelId){
            return ResponseResult.errorResult(AppHttpCodeEnum.EXCEL_REPORT_FAIL);
        }
        int count=-1;
         count = labelMapper.delete(Wrappers.<Label>lambdaQuery().eq(Label::getId, labelId));
        if (count==-1){
            return ResponseResult.errorResult(AppHttpCodeEnum.EXCEL_REPORT_FAIL);
        }

            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updatechannel(WmChannel wmChannel) {
        if (null == wmChannel) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
//        Label selectOne = labelMapper.selectOne(Wrappers.<Label>lambdaQuery().eq(Label::getName, wmChannel.getName()));
//        if (null != selectOne) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道不存在无法修改");
//        }
        WmChannel channel = new WmChannel();
        BeanUtils.copyProperties(wmChannel, channel);
        adminClient.updatechannel(channel);
        Label label = new Label();
        BeanUtils.copyProperties(wmChannel, label);
        labelMapper.updateById(label);
        ChannelLabel channelLabel = new ChannelLabel();
        BeanUtils.copyProperties(wmChannel, channelLabel);
        this.updateById(channelLabel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
